﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Configuration;

namespace FullRefreshService
{
    public static class ConfigHelper
    {
        public static T GetValue<T>(ConfigOption option, T defaultValue)
        {
            T retVal = defaultValue;
            var value = ConfigurationManager.AppSettings[option.ToString()];
            if (!string.IsNullOrEmpty(value))
            {
                var converter = TypeDescriptor.GetConverter(typeof(T));
                if (converter.IsValid(value))
                    retVal = (T)converter.ConvertFrom(value);
            }

            return retVal;
        }

        public static void SetValue(ConfigOption option, object value)
        {
            ConfigurationManager.AppSettings[option.ToString()] = value.ToString();
        }
    }
}
